package com.moive.news.tqnewsapplication.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.base.AppManager;
import com.moive.news.tqnewsapplication.base.BaseActivity;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PackageUtils;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/12.
 */
public class SettingActivity extends BaseActivity {
    @BindView(R.id.setting_back)
    ImageView settingBack;
    @BindView(R.id.setting_push_img)
    ImageView settingPushImg;
    @BindView(R.id.setting_push_relate)
    RelativeLayout settingPushRelate;
    @BindView(R.id.setting_feedback)
    ImageView settingFeedback;
    @BindView(R.id.setting_feedback_relate)
    RelativeLayout settingFeedbackRelate;
    @BindView(R.id.setting_help)
    ImageView settingHelp;
    @BindView(R.id.setting_help_relate)
    RelativeLayout settingHelpRelate;
    @BindView(R.id.setting_about)
    ImageView settingAbout;
    @BindView(R.id.setting_about_relate)
    RelativeLayout settingAboutRelate;
    @BindView(R.id.setting_version)
    ImageView settingVersion;
    @BindView(R.id.setting_version_relate)
    RelativeLayout settingVersionRelate;
    @BindView(R.id.exit_login_button)
    Button exitLoginButton;
    @BindView(R.id.push_toggle)
    ToggleButton pushToggle;
    private static final String TAG = "SettingActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        settingBack.setOnClickListener(v -> finish());
    }

    @OnClick({R.id.setting_push_relate, R.id.setting_feedback_relate, R.id.setting_help_relate, R.id.setting_about_relate, R.id.setting_version_relate, R.id.exit_login_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.setting_push_relate:
                if (pushToggle.isChecked()==true){
                    JPushInterface.stopPush(getApplicationContext());
                }else {
                    JPushInterface.resumePush(getApplicationContext());
                }
                break;
            case R.id.setting_feedback_relate:
                startActivity(new Intent(SettingActivity.this, FeedbackActivity.class));
                break;
            case R.id.setting_help_relate:
                startActivity(new Intent(SettingActivity.this, HelpActivity.class));
                break;
            case R.id.setting_about_relate:
                startActivity(new Intent(SettingActivity.this, AboutActivity.class));
                break;
            case R.id.setting_version_relate:
                String version = PackageUtils.getAppVersionName(SettingActivity.this);
                ToastUtils.ShortToast("当前版本信息：" + version);
                break;
            case R.id.exit_login_button:
                deltetoken();
                break;
        }
    }
    private void deltetoken() {
        Intent intent=new Intent(SettingActivity.this,LoginActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("from","2");
        intent.putExtras(bundle);
//        Platform qq = ShareSDK.getPlatform( QQ.NAME);
//        Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
//        Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
//        if (qq.isAuthValid()) {
//            qq.removeAccount(true);
//        }
//        if (wechat.isAuthValid()) {
//            wechat.removeAccount(true);
//        }
//        if (weibo.isAuthValid()) {
//            weibo.removeAccount(true);
//        }
        if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
            ToastUtils.ShortToast("退出登录成功");
            startActivity(intent);
            AppManager.getAppManager().finishAllActivity();
        } else {
            String token = PreferenceUtil.getString("token", "");
            OkHttpUtils.post()
                    .url(BaseApi.DELTEToken)
                    .addParams("token", token)
                    .build().execute(new OkManagerCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    LogUtil.e(TAG, e.toString());
                }
                @Override
                public void onResponse(String response, int id) {
                    super.onResponse(response, id);
                    LogUtil.i(TAG, response.toString());
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (jsonObject.optInt("code") == 0) {
                            ToastUtils.ShortToast("退出登录成功");
                            PreferenceUtil.put("id", "");
//                            PreferenceUtil.put("telephone", "");
                            PreferenceUtil.put("headImg", "");
                            PreferenceUtil.put("sex", "");
                            PreferenceUtil.put("nickName", "");
                            PreferenceUtil.put("token", "");
                            startActivity(intent);
                            AppManager.getAppManager().finishAllActivity();
                        }else {
                            PreferenceUtil.put("id", "");
//                            PreferenceUtil.put("telephone", "");
//                            PreferenceUtil.put("headImg", "");
                            PreferenceUtil.put("sex", "");
                            PreferenceUtil.put("nickName", "");
                            PreferenceUtil.put("token", "");
                            startActivity(intent);
                            AppManager.getAppManager().finishAllActivity();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
